package org.leo.dm.component.writer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;

import org.leo.dm.common.bean.StepResultBean;
import org.leo.dm.component.base.IWriter;
import org.leo.dm.component.callback.Callback;
import org.leo.dm.component.callback.DefaultCallback;
import org.leo.dm.component.engine.Storage;
import org.leo.dm.component.flow.Flow;
import org.leo.dm.util.ThreadPoolUtil;
import org.leo.dm.util.ToUtil;

/**
 * 
 * Created on 2016年2月17日
 * <p> 
 * Description 写组件抽象类
 * <p> 
 * Copyright neusoft (c) 2015 .
 * 
 * @author xiao_h 
 *
 */
public abstract class Writer implements IWriter, Callable<StepResultBean> {

	protected Writer() {
		setThreadPoolSize(0);
		util = new ToUtil();
		// 初始化相关插件
		this.callback = new DefaultCallback();
	}

	/**
	 * 线程池工具类
	 */
	protected ThreadPoolUtil tpu;

	/**
	 * 计数器
	 */
	protected int counter;

	private boolean runFlag = true;

	/*
	 * 暂留 字段 private boolean initBlockFlag;
	 * 
	 * public boolean isInitBlockFlag() { return initBlockFlag; }
	 * 
	 * public void setInitBlockFlag(boolean initBlockFlag) { this.initBlockFlag = initBlockFlag; }
	 */

	public boolean isRunFlag() {
		return runFlag;
	}

	public void setRunFlag(boolean runFlag) {
		this.runFlag = runFlag;
	}

	private Flow flow;

	public Flow getFlow() {
		return flow;
	}

	public void setFlow(Flow flow) {
		this.flow = flow;
	}

	protected ToUtil util;

	protected void setThreadPoolSize(int size) {
		if (size == 0) {
			size = 1;
		}
		tpu = new ThreadPoolUtil();
		tpu.initPool(1);
	}

	public Map<String, Object> getDaemonData() {
		Map<String, Object> daemonData = new HashMap<String, Object>();
		util.getDuration();
		daemonData.put("countms", new Double(counter / util.getDuration()).intValue());
		daemonData.put("counter", counter);
		daemonData.put("time", util.getDuration());
		return daemonData;
	}

	protected List<Future<StepResultBean>> resultList = new ArrayList<Future<StepResultBean>>();

	protected Storage storage;

	protected Callback callback;

	private StepResultBean resultBean;

	public StepResultBean getResultBean() throws Exception {
		resultBean = resultList.get(0).get();
		return resultBean;
	}

	protected void setResultBean(StepResultBean resultBean) {
		this.resultBean = resultBean;
	}

	public void setCallback(Callback callback) {
		this.callback = callback;
	}

	public void setStorage(Storage storage) {
		this.storage = storage;
	}

	protected Object[] targetDataNames;

	protected Object[] getTargetDataNames() {
		return targetDataNames;
	}

	protected void setTargetDataNames(Object[] targetDataNames) {
		this.targetDataNames = targetDataNames;
	}

	protected Object[] targetDataValues;

	protected Object[] getTargetDataValues() {
		return targetDataValues;
	}

	protected void setTargetDataValues(Object[][] targetDataValues) {
		this.targetDataValues = targetDataValues;
	}

	@Override
	public void handle() {
		counter = 0;
		Future<StepResultBean> future = tpu.getPool().submit(this);
		resultList.add(future);
		tpu.shutdownPool();
	}

}
